import { CustPageCmpDesignInfo, BaseDesignInfo, TYPE_KEY_CLS } from '@/interfaceCom/iCustomPage'

const elementComponentDict = new Map<string, string>()
const mdlCmpDesignInfos: any[] = []
const mdlClsNameDesignInfoDict = new Map<string, BaseDesignInfo>()

export default class ModelUtils {
    public static regCustPageEleCmp(componentName: string, designInfo: CustPageCmpDesignInfo) {
        this.regCustMdlCmp(componentName, designInfo)
    }
    private static regCustMdlCmp(componentName: string, designInfo: BaseDesignInfo) {
        const eleCls = designInfo.elementClass
        const eleRegName = this.getRegisterName(eleCls.prototype)
        if (!eleRegName) {
            throw new Error('未找到注册的元素')
        }
        if (elementComponentDict.has(eleRegName)) {
            throw new Error(`元素[${eleRegName}]已经注册到组件[${elementComponentDict.get(eleRegName)}]，无法再次注册到组件[${componentName}]`)
        }
        elementComponentDict.set(eleRegName, componentName)
        mdlCmpDesignInfos.push(designInfo)
        mdlClsNameDesignInfoDict.set(eleRegName, designInfo)
    }
    /**
     * 通过类获取类的别名
     *
     * @static
     * @param {*} obj
     * @returns {(string | undefined)}
     * @memberof FormFactory
     */
    public static getRegisterName(obj: any): string | undefined {
        if (!this.isPrimaryType(obj)) {
            return obj[TYPE_KEY_CLS]
        }
    }
    /**
     * 是否为基础类型： undefined, null, boolean, string, number
     *
     * @static
     * @param {*} obj
     * @returns {boolean}
     * @memberof FormUtils
     */
    public static isPrimaryType(obj: any): boolean {
        return obj !== Object(obj)
    }
    /**
     * 获取自定义页面组件全部的设计信息
     * @returns {CustPageCmpDesignInfo[]}
     */
    public static getCustPageDesignInfos(): any[] {
        // return mdlCmpDesignInfos.filter(d => d.custType === 'page')
        return mdlCmpDesignInfos
    }

    /**
     * 自定义表单，根据元素类型，返回Vue组件的名字
     * @param elementType
     */
    public static getCompNameByEleType(eleType: string): string {
        const tmp = elementComponentDict.get(eleType)
        return tmp || eleType
    }
}
